package com.example.springbott.service;

import com.example.springbott.bean.MarketRegion;
import com.example.springbott.bean.MarketRegionExample;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketRegionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author silic xia
 * @since 2022/11/21 11:04
 */
@Service
public class AdminRegionServiceImpl implements AdminRegionService {

    @Autowired
    MarketRegionMapper regionMapper;

    /**
     * 行政区域
     * type=1 , id=pid/type=2, id=pid/type=3
     * @param
     * @return
     * @author silic xia
     * @since 2022/11/21 11:12
     */
    @Override
    public CommonData<MarketRegion> adminRegionList() {

        // 省
        MarketRegionExample example = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo((byte) 1);
        List<MarketRegion> marketRegions = regionMapper.selectByExample(example);

        for (MarketRegion marketRegion : marketRegions) {
            // 市
            MarketRegionExample example1 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria1 = example1.createCriteria();
            Integer id = marketRegion.getId();
            criteria1.andTypeEqualTo(((byte) 2));
            criteria1.andPidEqualTo(id);
            List<MarketRegion> marketRegions1 = regionMapper.selectByExample(example1);
            marketRegion.setChildren(marketRegions1);

            for (MarketRegion region : marketRegions1) {
                // 区
                MarketRegionExample example2 = new MarketRegionExample();
                MarketRegionExample.Criteria criteria2 = example2.createCriteria();
                id = region.getId();
                criteria2.andTypeEqualTo(((byte) 3));
                criteria2.andPidEqualTo(id);
                List<MarketRegion> marketRegions2 = regionMapper.selectByExample(example2);
                region.setChildren(marketRegions2);
            }
        }
        PageHelper.startPage(1,marketRegions.size());
        PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions);
        return CommonData.data(pageInfo);
    }


}

